/*
 * This file is part of the OdinMS Maple Story Server
 * Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
 *                    Matthias Butz <matze@odinms.de>
 *                    Jan Christian Meyer <vimes@odinms.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License version 3
 * as published by the Free Software Foundation. You may not use, modify
 * or distribute this program under any other version of the
 * GNU Affero General Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* Author: Xterminator
	NPC Name: 		Arwen the Fairy
	Map(s): 		Victoria Road : Ellinia (101000000)
	Description: 		Refines Moon Rock, Star Rock and Black Feather
*/
var status = 0;
var items = Array(4011007, 4021009, 4031042);
var mesos = Array(10000, 15000, 30000);
var selected;

function start() {
	status = -1;
	action(1, 0, 0);
}

function action(mode, type, selection) {
	if (mode == -1) {
		cm.dispose();
	} else {
		if (mode == 0) {
			if (status == 2) {
				cm.sendNext("It's not easy making #t" + items[selected] + "#. Please get the materials ready.");
			}
			cm.dispose();
			return;
		}
		if (mode == 1)
			status++;
		else
			status--;
		if (status == 0) {
			if (cm.getPlayer().getLevel() >= 40) {
				cm.sendNext("Yeah... I am the master alchemist of the fairies. But the fairies are not supposed to be in contact with a human being for a long period of time... A strong person like you will be fine, though. If you get me the materials, I'll make you a special item.");
			} else {
				cm.sendOk("I can make rare, valuable items but unfortunately I can't make it to a stranger like you.");
				cm.dispose();
			}
		} else if (status == 1) {
			var selStr = "What do you want to make?#b";
			for (var i = 0; i < items.length; i++) {
				selStr += "\r\n#L" + i + "##t" + items[i] + "##l";
			}
			cm.sendSimple(selStr);
		} else if (status == 2) {
			selected = selection;
			if (selection == 0) {
				cm.sendYesNo("So you want to make #t" + items[selected] + "#? To do that you need refined one of each of these: #b#t4011000##k, #b#t4011001##k,\r\n#b#t4011002##k, #b#t4011003##k, #b#t4011004##k, #b#t4011005##k and #b#t4011006##k. Throw in 10,000 mesos and I'll make it for you.");
			} else if (selection == 1) {
				cm.sendYesNo("So you want to make #t" + items[selected] + "#? To do that you need refined one of each of these: #bGarnet#k, #bAmethyst#k, #bAquaMarine#k, #bEmerald#k, #bOpal#k, #bSapphire#k, #bTopaz#k, #bDiamond#k and #bBlack Crystal#k. Throw in 15,000 mesos and I'll make it for you.");
			} else if (selection == 2) {
				cm.sendYesNo("So you want to make #t" + items[selected] + "#? To do that you need #b1 Flaming Feather#k, #b1 Moon Rock#k and #b1 Black Crystal#k. Throw in 30,000 mesos and I'll make it for you. Oh yeah, this piece of feather is a very special item, so if you drop it by any chance, it'll disappear, as well as you won't be able to give it away to someone else.");
			}
		} else if (status == 3) {
			var haveItems = 0;
			var materialSet = Array(Array(4011000, 4011001, 4011002, 4011003, 4011004, 4011005, 4011006), Array(4021000, 4021001, 4021002, 4021003, 4021004, 4021005, 4021006, 4021007, 4021008), Array(4001006, 4011007, 4021008));
			var materials = materialSet[selected];
			for (var i = 0; i < materials.length; i++) {
				if (cm.haveItem(materials[i]) && cm.getPlayer().getMeso() >= mesos[selected]) {
					haveItems += 1;
				}
			}
			if (haveItems == materials.length && cm.canHold(items[selected])) {
				cm.gainMeso(-mesos[selected]);
				for (var i = 0; i < materials.length; i++) {
					cm.gainItem(materials[i], -1);
				}
				cm.gainItem(items[selected], 1);
				cm.sendNext("Ok here, take #t" + items[selected] + "#. It's well-made, probably because I'm using good materials. If you need my help down the road, feel free to come back.");
			} else {
				if (selected != 2) {
					var selStr = "Are you sure you have enough mesos? Please check and see if you have the refined ";
					for (var i = 0; i < materials.length; i++) {
						selStr += "#b#t" + materials[i] + "##k, ";
					}
					cm.sendNext(selStr + "one of each.");
				} else {
					var selStr = "Are you sure you have enough mesos? Please check and see if you have ";
					for (var i = 0; i < materials.length - 1; i++) {
						selStr += "#b1 #t" + materials[i] + "##k, ";
					}
					cm.sendNext(selStr + "and #b1 #t" + materials[materials.length - 1] + "##k ready for me.");
				}
			}
			cm.dispose();
		}
	}
}